/*
 * Copyright 2011 Guidebee Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 */

//--------------------------------- PACKAGE ------------------------------------
package com.mapdigit.practices.compositeui;

//--------------------------------- IMPORTS ------------------------------------
//[------------------------------ MAIN CLASS ----------------------------------]
////////////////////////////////////////////////////////////////////////////////
//--------------------------------- REVISIONS ----------------------------------
// Date       Name                 Tracking #         Description
// --------   -------------------  -------------      --------------------------
// 13AUG2011  James Shen                 	      Code review
////////////////////////////////////////////////////////////////////////////////
/**
 * An implementation of IWorkItemActivationService that ensures that only
 * WorkItem is active at one time.
 * <P>
 * <hr>
 * <hr><b>&copy; Copyright 2011 Guidebee Pty Ltd. All Rights Reserved.</b>
 * @version     1.00, 13/08/11
 * @author      Guidebee Pty Ltd.
 */
public class SimpleWorkItemActivationService 
    implements IWorkItemActivationService {
    
    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 13AUG2011  James Shen                 	          Code review
    ////////////////////////////////////////////////////////////////////////////
    /**
     * Initializes a new instance of the SimpleWorkItemActivationService class.
     */
    public SimpleWorkItemActivationService() {
    }

    ////////////////////////////////////////////////////////////////////////////
    //--------------------------------- REVISIONS ------------------------------
    // Date       Name                 Tracking #         Description
    // ---------  -------------------  -------------      ----------------------
    // 13AUG2011  James Shen                 	          Code review
    ////////////////////////////////////////////////////////////////////////////
    /**
     * See IWorkItemActivationService.ChangeStatus for more information.
     * @param item work item need to change status.
     */
    public void ChangeStatus(WorkItem item) {
        synchronized (syncroot) {
            if (item != activeWorkItem 
                    && item.getStatus() == WorkItemStatus.Active) {
                if (activeWorkItem != null
                        && activeWorkItem.getStatus() 
                        != WorkItemStatus.Terminated) {
                    activeWorkItem.deactivate();
                }

                activeWorkItem = item;
            }
        }
    }
    
    
    private final Object syncroot = new Object();
    private WorkItem activeWorkItem;
}
